package com.xbai.goods.controller;

import com.github.pagehelper.PageInfo;
import com.xbai.entity.Result;
import com.xbai.goods.pojo.Spec;
import com.xbai.goods.service.SpecService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author jxbai
 * @desc
 * @date 2021/1/27
 */
@RestController
@RequestMapping("/spec")
@CrossOrigin
@RequiredArgsConstructor
public class SpecController {

    private final SpecService specService;

    @GetMapping("/category/{id}")
    public Result<List<Spec>> findByCategoryId(@PathVariable("id") Integer categoryId) {
        List<Spec> specList = specService.findByCategoryId(categoryId);
        return new Result<>(specList);
    }

    @PostMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@RequestBody(required = false)  Spec spec, @PathVariable  int page, @PathVariable  int size){
        //调用SpecService实现分页条件查询Spec
        PageInfo<Spec> pageInfo = specService.findPage(spec, page, size);
        return new Result<>(pageInfo);
    }

    @GetMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@PathVariable  int page, @PathVariable  int size){
        //调用SpecService实现分页查询Spec
        PageInfo<Spec> pageInfo = specService.findPage(page, size);
        return new Result<>(pageInfo);
    }

    @PostMapping(value = "/search" )
    public Result<List<Spec>> findList(@RequestBody(required = false)  Spec spec){
        //调用SpecService实现条件查询Spec
        List<Spec> list = specService.findList(spec);
        return new Result<>(list);
    }

    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable Integer id){
        //调用SpecService实现根据主键删除
        specService.delete(id);
        return new Result();
    }

    @PutMapping(value="/{id}")
    public Result update(@RequestBody  Spec spec,@PathVariable Integer id){
        //设置主键值
        spec.setId(id);
        //调用SpecService实现修改Spec
        specService.update(spec);
        return new Result();
    }

    @PostMapping
    public Result add(@RequestBody   Spec spec){
        //调用SpecService实现添加Spec
        specService.add(spec);
        return new Result();
    }

    @GetMapping("/{id}")
    public Result<Spec> findById(@PathVariable Integer id){
        //调用SpecService实现根据主键查询Spec
        Spec spec = specService.findById(id);
        return new Result<>(spec);
    }

    @GetMapping
    public Result<List<Spec>> findAll(){
        //调用SpecService实现查询所有Spec
        List<Spec> list = specService.findAll();
        return new Result<>(list) ;
    }
}
